﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AdaptService.cs" company="">
//   
// </copyright>
// <summary>
//   The adapt service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Adapters
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    ///   The adapt service.
    /// </summary>
    public class AdaptService : IAdaptService
    {
        #region Fields

        /// <summary>
        ///   The _adapter factories.
        /// </summary>
        private readonly Dictionary<Type, Dictionary<Type, IAdapterFactory>> _adapterFactories =
            new Dictionary<Type, Dictionary<Type, IAdapterFactory>>();

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The add adapter factory.
        /// </summary>
        /// <param name="factoryFunc">
        /// The factory func. 
        /// </param>
        /// <typeparam name="T1">
        /// </typeparam>
        /// <typeparam name="T2">
        /// </typeparam>
        public void AddAdapterFactory<T1, T2>(Func<T1, T2> factoryFunc)
        {
            this.AddAdapterFactory(new FuncAdapterFactory<T1, T2>(factoryFunc));
        }

        /// <summary>
        /// The can adapt.
        /// </summary>
        /// <param name="type1">
        /// The type 1. 
        /// </param>
        /// <param name="type2">
        /// The type 2. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public bool CanAdapt(Type type1, Type type2)
        {
            Dictionary<Type, IAdapterFactory> typeAdapterFactories;
            if (this._adapterFactories.TryGetValue(type1, out typeAdapterFactories))
            {
                return typeAdapterFactories.ContainsKey(type2);
            }

            return false;
        }

        /// <summary>
        /// The get adapt types.
        /// </summary>
        /// <param name="type">
        /// The type. 
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable"/> . 
        /// </returns>
        public IEnumerable<Type> GetAdaptTypes(Type type)
        {
            Dictionary<Type, IAdapterFactory> typeAdapterFactories;
            if (this._adapterFactories.TryGetValue(type, out typeAdapterFactories))
            {
                return typeAdapterFactories.Keys;
            }

            return new List<Type>();
        }

        /// <summary>
        /// The try adapt.
        /// </summary>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="type">
        /// The type. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public bool TryAdapt(object obj1, Type type, out object obj2)
        {
            Dictionary<Type, IAdapterFactory> typeAdapterFactories;
            if (this._adapterFactories.TryGetValue(obj1.GetType(), out typeAdapterFactories))
            {
                IAdapterFactory factory;
                if (typeAdapterFactories.TryGetValue(type, out factory))
                {
                    obj2 = factory.CreateAdapter(obj1);
                    return true;
                }
            }

            obj2 = null;
            return false;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The add adapter factory.
        /// </summary>
        /// <param name="adapterFactory">
        /// The adapter factory. 
        /// </param>
        protected void AddAdapterFactory(IAdapterFactory adapterFactory)
        {
            Dictionary<Type, IAdapterFactory> typeAdapterFactories;
            if (!this._adapterFactories.TryGetValue(adapterFactory.Type1, out typeAdapterFactories))
            {
                typeAdapterFactories = new Dictionary<Type, IAdapterFactory>();
                this._adapterFactories.Add(adapterFactory.Type1, typeAdapterFactories);
            }

            typeAdapterFactories.Add(adapterFactory.Type2, adapterFactory);
        }

        #endregion
    }
}